home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8352 / 8352.xpi / chrome / greasefire.jar / content / test / test.js < prev   
Text File  |  2009-01-06  |  4KB  |  195 lines

  1. /*
  2.  * Copyright (C) 2008 by Steve Krulewitz <skrulx@gmail.com>
  3.  * Licensed under GPLv2 or later, see file LICENSE in the xpi for details.
  4.  */
  5. const Cc = Components.classes;
  6. const Ci = Components.interfaces;
  7. const Cr = Components.results;
  8.  
  9. var bis;
  10. var seekable;
  11. var cache = [];
  12.  
  13. function runTest() {
  14.  
  15.   var method = 2;
  16.  
  17.   var urls = [
  18.     "http://reddit.com/",
  19.     "http://slashdot.org/",
  20.     "http://nytimes.com/",
  21.     "http://www.cnn.com/blah",
  22.     "http://google.com",
  23.     "http://www.google.com",
  24.     "http://myspace.com",
  25.     "http://facebook.com"
  26.   ];
  27.   var o = {};
  28.  
  29.   var t = Date.now();
  30.  
  31.   if (method == 1 || method == 2) {
  32.     var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  33.     file.initWithPath("/home/steve/dev/greasefire/index.dat");
  34.  
  35.     var fis = Cc["@mozilla.org/network/file-input-stream;1"]
  36.     .createInstance(Ci.nsIFileInputStream);
  37.     fis.init(file, -1, 0, 0);
  38.     var buffer = Cc["@mozilla.org/network/buffered-input-stream;1"]
  39.     .createInstance(Ci.nsIBufferedInputStream);
  40.     buffer.init(fis, 4096);
  41.  
  42.     bis = Cc["@mozilla.org/binaryinputstream;1"]
  43.     .createInstance(Ci.nsIBinaryInputStream);
  44.     bis.setInputStream(buffer);
  45.  
  46.     //  var junk = [];
  47.     //  bis.readByteArray(file.fileSize, junk, {});
  48.  
  49.     seekable = buffer.QueryInterface(Ci.nsISeekableStream);
  50.   }
  51.   else {
  52.     var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
  53.     .getService(Ci.mozIJSSubScriptLoader);
  54.     loader.loadSubScript("file:///home/steve/dev/greasefire/index.json", o);
  55.   }
  56.  
  57.   var d = Date.now() - t;
  58.   log("startup " + d + "ms");
  59.  
  60.   for (var i = 0; i < urls.length; i++) {
  61.     var matches = [];
  62.     var t = Date.now();
  63.     switch (method) {
  64.     case 1: find1(urls[i], 0, 0, matches); break
  65.     case 2: find2(urls[i], 0, 0, matches); break;
  66.     case 3: find3(o.data, urls[i], 0, matches); break;
  67.  
  68.     }
  69.     var d = Date.now() - t;
  70.  
  71.     var m = {};
  72.     var count = 0;
  73.     for (var j = 0; j < matches.length; j++) {
  74.       if (!(matches[j] in m)) {
  75.         count++;
  76.         m[matches[j]] = 1;
  77.       }
  78.     }
  79.  
  80.     log("--> " + urls[i] + " matches " + count + " " + d + "ms");
  81.   }
  82.  
  83.   return true;
  84. }
  85.  
  86. function find1(url, indexPos, stringPos, matches) {
  87.  
  88.   seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, indexPos);
  89.  
  90.   var idsLength = bis.read64();
  91.   for (var i = 0; i < idsLength; i++) {
  92.     matches.push(bis.read64());
  93.   }
  94.  
  95.   if (stringPos > url.length - 1) {
  96.     return;
  97.   }
  98.  
  99.   var childrenLength = bis.read16();
  100.   for (var i = 0; i < childrenLength; i++) {
  101.     var c = String.fromCharCode(bis.read16());
  102.     var pos = bis.read64();
  103.     if (c == "*") {
  104.       for (var j = stringPos; j < url.length; j++) {
  105.         var tell = seekable.tell();
  106.         find1(url, pos, j, matches);
  107.         seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, tell);
  108.       }
  109.     }
  110.     else if (url.charAt(stringPos) == c) {
  111.       var tell = seekable.tell();
  112.       find1(url, pos, stringPos + 1, matches);
  113.       seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, tell);
  114.     }
  115.   }
  116. }
  117.  
  118. function find2(url, indexPos, stringPos, matches) {
  119.  
  120.   var o = cache[indexPos];
  121.   if (!o) {
  122.     o = {};
  123.     cache[indexPos] = o;
  124.  
  125.     seekable.seek(Ci.nsISeekableStream.NS_SEEK_SET, indexPos);
  126.  
  127.     var idsLength = bis.read64();
  128.     if (idsLength > 0) {
  129.       var ids = [];
  130.       for (var i = 0; i < idsLength; i++) {
  131.         ids.push(bis.read64());
  132.       }
  133.       o[" "] = ids;
  134.     }
  135.  
  136.     var childrenLength = bis.read16();
  137.     for (var i = 0; i < childrenLength; i++) {
  138.       var c = String.fromCharCode(bis.read16());
  139.       var pos = bis.read64();
  140.       o[c] = pos;
  141.     }
  142.   }
  143.  
  144.   var ids = o[" "];
  145.   if (ids) {
  146.     for (var i = 0; i < ids.length; i++) {
  147.       matches.push(ids[i]);
  148.     }
  149.   }
  150.  
  151.   if (stringPos > url.length - 1) {
  152.     return;
  153.   }
  154.  
  155.   var nextPos = o[url.charAt(stringPos)];
  156.   if (nextPos) {
  157.     find2(url, nextPos, stringPos + 1, matches);
  158.   }
  159.  
  160.   var wildPos = o["*"];
  161.   if (wildPos) {
  162.     for (var i = 0; i < url.length; i++) {
  163.       find2(url, wildPos, i, matches);
  164.     }
  165.   }
  166.  
  167. }
  168.  
  169. function find3(data, url, stringPos, matches) {
  170.  
  171.   var ids = data[" "];
  172.   if (ids) {
  173.     for (var i = 0; i < ids.length; i++) {
  174.       matches.push(ids[i]);
  175.     }
  176.   }
  177.  
  178.   if (stringPos > url.length - 1) {
  179.     return;
  180.   }
  181.  
  182.   var next = data[url.charAt(stringPos)];
  183.   if (next) {
  184.     find3(next, url, stringPos + 1, matches);
  185.   }
  186.  
  187.   var wild = data["*"];
  188.   if (wild) {
  189.     for (var i = 0; i < url.length; i++) {
  190.       find3(wild, url, i, matches);
  191.     }
  192.   }
  193.  
  194. }
  195.